package com.cl.cloud.binlog.filter;

import com.cl.cloud.binlog.annotation.BLEntity;
import com.cl.cloud.binlog.binlogEntity.BinLogEntityHandle;
import lombok.Getter;
import org.springframework.core.annotation.AnnotationUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 数据库表过滤
 * @author: liuzijian
 * @time: 2021-12-21 16:17
 */
public class TableFilter {

    @Getter
    private final Map<Class<? extends BinLogEntityHandle>, String> entityClassMap = new HashMap<>();
    @Getter
    private final Map<String, Class<? extends BinLogEntityHandle>> tableNameMap = new HashMap<>();

    public TableFilter registTable(Class<? extends BinLogEntityHandle> clazz) {
        BLEntity blEntity = AnnotationUtils.findAnnotation(clazz, BLEntity.class);
        if (blEntity == null) {
            return this;
        }
        if (tableNameMap.get(blEntity.value()) == null) {
            tableNameMap.put(blEntity.value(), clazz);
            entityClassMap.put(clazz, blEntity.value());
        }
        return this;
    }

    /**
     * 过滤
     *
     * @param clazz
     * @return false：不需要过滤 true：需要
     */
    public boolean filter(Class<?> clazz) {
        return entityClassMap.get(clazz) != null;
    }

    public boolean filter(String tableName) {
        return tableNameMap.get(tableName) != null;
    }
}
